package nimo.elibrary.actions;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.elibrary.BookstoreApplication;
import com.elibrary.R;
import com.google.gson.Gson;
import nimo.elibrary.Url;
import nimo.elibrary.WebService;
import nimo.elibrary.db.model.UserDataSource;
import nimo.elibrary.json.model.BaseResponse;
import nimo.elibrary.json.model.history.HistoryList;
import nimo.elibrary.json.model.user.getHistoryResponse;
import nimo.elibrary.json.model.user.signinResponse;
import nimo.elibrary.json.model.user.signoutResponse;
import nimo.elibrary.json.model.user.signupResponse;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Enkuso
 * Date: 12/10/12
 * Time: 11:49 AM
 * To change this template use File | Settings | File Templates.
 */
public class User {

    public static nimo.elibrary.json.model.user.User Signin(String username, String password)
    {

        // POST-р илгээх өгөгдлүүдийг оноох
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));

        InputStream source = WebService.retrieveStreamPOST(Url.signinURL, nameValuePairs);

        if(source != null){
            Gson gson = new Gson();

            Reader reader = new InputStreamReader(source);

            signinResponse response = gson.fromJson(reader, signinResponse.class);

            if(response.stat.equals(WebService.STATUS_OK)){
                // Амжилттай нэвтэрсэн
                nimo.elibrary.json.model.user.User user = response.user;

                return user;
            }
            else {
                // Нэвтэрч чадаагүй
                return null;
            }
        }
        else {
            // Сэрвэртэй холбогдож чадаагүй
            return null;
        }
    }

    public static nimo.elibrary.json.model.user.User Signup(String username, String email, String password)
    {
        // POST-р илгээх өгөгдлүүдийг оноох
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("email", email));
        nameValuePairs.add(new BasicNameValuePair("password", password));

        InputStream source = WebService.retrieveStreamPOST(Url.signupURL, nameValuePairs);


        if(source != null){
            Gson gson = new Gson();

            Reader reader = new InputStreamReader(source);

            signupResponse response = gson.fromJson(reader, signupResponse.class);

            if(response.stat.equals(WebService.STATUS_OK)){
                // Амжилттай бүртгүүлсэн
                nimo.elibrary.json.model.user.User user = response.user;

                return user;
            }
            else {
                // Бүртгүүлж чадаагүй
                return null;
            }
        }
        else {
            // Сэрвэртэй холбогдож чадаагүй
            return null;
        }
    }

    public static String getToken()
    {
        // TODO: Нэвтэрсэн хэрэглэгчийн token авах
        return "t0k3n4t3st";
    }

    public static boolean editProfile(String username, String email, String password, String first_name, String last_name)
    {
        // POST-р илгээх өгөгдлүүдийг оноох
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("email", email));
        nameValuePairs.add(new BasicNameValuePair("password", password));
        nameValuePairs.add(new BasicNameValuePair("firstname", first_name));
        nameValuePairs.add(new BasicNameValuePair("lastname", last_name));
        nameValuePairs.add(new BasicNameValuePair("token", getToken()));

        InputStream source = WebService.retrieveStreamPOST(Url.editProfileURL, nameValuePairs);


        if(source != null){
            Gson gson = new Gson();

            Reader reader = new InputStreamReader(source);

            signupResponse response = gson.fromJson(reader, signupResponse.class);

            if(response.stat.equals(WebService.STATUS_OK)){
                // Амжилттай хадгалсан
                nimo.elibrary.json.model.user.User user = response.user;

                return true;
            }
            else {
                // Хадгалж чадаагүй
                return false;
            }
        }
        else {
            // Сэрвэртэй холбогдож чадаагүй
            return false;
        }
    }

    public static boolean Signout()
    {
        // POST-р илгээх өгөгдлүүдийг оноох
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("token", getToken()));

        InputStream source = WebService.retrieveStream(Url.signoutURL, nameValuePairs);


        if(source != null){
            Gson gson = new Gson();

            Reader reader = new InputStreamReader(source);

            signoutResponse response = gson.fromJson(reader, signoutResponse.class);

            if(response.stat.equals(WebService.STATUS_OK)){
                // Амжилттай гарсан
                return true;
            }
            else {
                // Unknown error
                return false;
            }
        }
        else {
            // Сэрвэртэй холбогдож чадаагүй
            return false;
        }
    }

    public static boolean ResetPassword(String email)
    {
        // POST-р илгээх өгөгдлүүдийг оноох
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("email", email));

        InputStream source = WebService.retrieveStreamPOST(Url.resetPasswordURL, nameValuePairs);


        if(source != null){
            Gson gson = new Gson();

            Reader reader = new InputStreamReader(source);

            BaseResponse response = gson.fromJson(reader, BaseResponse.class);

            if(response.stat.equals(WebService.STATUS_OK)){
                // Нууц үг сэргээгдсэн
                return true;
            }
            else {
                // Алдаа
                return false;
            }
        }
        else {
            // Сэрвэртэй холбогдож чадаагүй
            return false;
        }
    }

    public static HistoryList getHistoryList(String token, String page)
    {

        // GET-р илгээх өгөгдлүүдийг оноох
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("token", token));
        nameValuePairs.add(new BasicNameValuePair("page", page));

        InputStream source = WebService.retrieveStream(Url.apiBaseUrl + "/user/getHistory." + Url.responseType, nameValuePairs);

        if(source != null){
            Gson gson = new Gson();

            Reader reader = new InputStreamReader(source);

            getHistoryResponse response = gson.fromJson(reader, getHistoryResponse.class);

            if(response.stat.equals(WebService.STATUS_OK)){
                HistoryList booklist = response.result;

                return booklist;
            }
            else {
                // Book not found
                return null;
            }
        }
        else {
            // Сэрвэртэй холбогдож чадаагүй
            return null;
        }
    }
}
